﻿using System.Collections.Generic;
using System.Windows.Forms;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.SqlConnector;

namespace ProjectManager.Masters.ModuleData
{
    internal class RadarDescriptionsParam : RadarParameters
    {
        public RadarDescriptionsParam(DescriptionType type, string code)
        {
            Params = new List<object>();
            Params.Add(type);
            Params.Add(code);
        }
    }

    internal class RadarProjectsParam : RadarParameters
    {
        public RadarProjectsParam(Counters type, string code)
        {
            Params = new List<object>();
            Params.Add(type);
            Params.Add(code);
        }
    }

    internal class RadarSessionsParam : RadarParameters
    {
        public RadarSessionsParam(int ID)
        {
            Params = new List<object>();
            Params.Add(ID);
        }
    }

    #region Radar Descriptions
    internal class RadarDescriptions : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter p1, p2, p3;
        private DescriptionType type;

        public RadarDescriptions(DescriptionType type)
            : base()
        {
            this.type = type;
            rdrCodeColumn = PM_Descriptions.Code.Name;
            rdrDescColumn = PM_Descriptions.Description.Name;
            switch (type)
            {
                case DescriptionType.E_Products:
                    rdrNameSpace = "ProjectManager.Masters.Forms.Products";
                    break;
            }
        }

        protected override Table GetRadarTable()
        {
            return new PM_Descriptions();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            qb.Clear();
            qb.AddSelectAll(PM_Descriptions.Name);
            qb.AddFrom(PM_Descriptions.Name);
            qb.AddCompare(PM_Descriptions.Type, "@p1");
            qb.AddCompare(PM_Descriptions.Code, "@p2");

            p1 = new SqlABParameter("@p1", PM_Descriptions.Type);
            p2 = new SqlABParameter("@p2", PM_Descriptions.Code);

            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(p1);
            sqlCmd.Parameters.Add(p2);

            return true;
        }

        protected override void PrepareFindQuery(IRadarParameters param)
        {
            p1.Value = param.Params[0];
            p2.Value = param.Params[1];
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            Description = sqlReader[PM_Descriptions.Description.Name].ToString();
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd, string findQuery)
        {
            p3 = new SqlABParameter("@p3", PM_Descriptions.Type);

            qb.Clear();
            qb.AddSelectAll(PM_Descriptions.Name);
            qb.AddFrom(PM_Descriptions.Name);
            qb.AddCompare(PM_Descriptions.Type, "@p3");
            qb.AddFilter(findQuery);

            sqlCmd.Parameters.Add(p3);

            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
            p3.Value = type;
        }

        protected override IRadarParameters PrepareRadarParameters(DataGridViewRow row)
        {
            return new RadarDescriptionsParam(type, DataGridViewRowValues.GetValue<string>(row, PM_Descriptions.Code));
        }

        public override IRadarParameters GetRadarParameters(string text)
        {
            return new RadarDescriptionsParam(type, text);
        }

        public override string GetCodeFromParameters(IRadarParameters param)
        {
            return param.GetValue<string>(1);
        }
    }

    #endregion Radar Descriptions

    #region RadarProjects
    internal class RadarProjects : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter p1, p2, p3;
        private Counters type;

        public RadarProjects(Counters type)
            : base()
        {
            this.type = type;
            this.ResourceManager = Properties.Resources.ResourceManager;

            rdrCodeColumn = PM_Project.Code.Name;
            rdrDescColumn = PM_Project.Description.Name;
            switch (type)
            {
                case Counters.Project:
                    rdrNameSpace = "ProjectManager.Masters.Forms.Projects";
                    break;
            }
        }

        protected override Table GetRadarTable()
        {
            return new PM_Project();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            qb.Clear();
            qb.AddSelectAll(PM_Project.Name);
            qb.AddFrom(PM_Project.Name);
            qb.AddCompare(PM_Project.Type, "@p1");
            qb.AddCompare(PM_Project.Code, "@p2");

            p1 = new SqlABParameter("@p1", PM_Project.Type);
            p2 = new SqlABParameter("@p2", PM_Project.Code);

            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(p1);
            sqlCmd.Parameters.Add(p2);

            return true;
        }

        protected override void PrepareFindQuery(IRadarParameters param)
        {
            p1.Value = param.Params[0];
            p2.Value = param.Params[1];
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            Description = sqlReader[PM_Project.Description.Name].ToString();
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd, string findQuery)
        {
            p3 = new SqlABParameter("@p3", PM_Project.Type);

            qb.Clear();
            qb.AddSelectAll(PM_Project.Name);
            qb.AddFrom(PM_Project.Name);
            qb.AddCompare(PM_Project.Type, "@p3");
            qb.AddFilter(findQuery);

            sqlCmd.Parameters.Add(p3);

            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
            p3.Value = type;
        }

        protected override IRadarParameters PrepareRadarParameters(DataGridViewRow row)
        {
            return new RadarProjectsParam(type, DataGridViewRowValues.GetValue<string>(row, PM_Project.Code));
        }

        public override IRadarParameters GetRadarParameters(string text)
        {
            return new RadarProjectsParam(type, text);
        }

        public override string GetCodeFromParameters(IRadarParameters param)
        {
            return param.GetValue<string>(1);
        }
    }
    #endregion RadarProjects

    #region RadarSessions
    internal class RadarSessions : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter p1;

        public RadarSessions()
            : base()
        {
            rdrCodeColumn = PM_Session.ID.Name;
            rdrDescColumn = PM_Session.Description.Name;
            rdrNameSpace = "ProjectManager.Masters.Forms.Sessions";
            ResourceManager = Properties.Resources.ResourceManager;
        }

        protected override Table GetRadarTable()
        {
            return new PM_Session();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            qb.Clear();
            qb.AddSelectAll(PM_Session.Name);
            qb.AddFrom(PM_Session.Name);
            qb.AddCompare(PM_Session.ID, "@p1");

            p1 = new SqlABParameter("@p1", PM_Session.ID);

            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(p1);

            return true;
        }

        protected override void PrepareFindQuery(IRadarParameters param)
        {
            p1.Value = param.Params[0];
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            Description = sqlReader[PM_Session.Description.Name].ToString();
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd, string findQuery)
        {
            qb.Clear();
            qb.AddSelectAll(PM_Session.Name);
            qb.AddFrom(PM_Session.Name);
            qb.AddFilter(findQuery);

            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
        }

        protected override IRadarParameters PrepareRadarParameters(DataGridViewRow row)
        {
            return new RadarSessionsParam(DataGridViewRowValues.GetValue<int>(row, PM_Session.ID));
        }

        public override IRadarParameters GetRadarParameters(string ID)
        {
            return new RadarSessionsParam(int.Parse(ID));
        }

        public override string GetCodeFromParameters(IRadarParameters param)
        {
            return param.GetValue<string>(1);
        }
    }
    #endregion RadarProjects
}